﻿(function ($) {

    var setline = function (target, settings) {
        var $target = $(target);
        var beginway = 1;
        var lineIndex = settings.lineIndex
        var style = settings.style;
        var start = { x: settings.startx, y: settings.starty };
        var end = { x: settings.endx, y: settings.endy };

        var size = { x: settings.startsize, y: settings.endsize };
        var ow, oh;
        if (size.x == "v") {
            ow = 10;

        }
        else {
            ow = 30;

        }
        if (size.y =="v") {

            oh = 30
        }
        else {

            oh = 10
        }


        $fline = $(".fline" + lineIndex + "");
        $sline = $(".sline" + lineIndex + "");
        $tline = $(".tline" + lineIndex + "");
        $arrow = $(".arrow" + lineIndex + "");
        $node = $(".node" + lineIndex + "");
        $container = $("#container")
        var exow, exoh;

        setup()



        function setup() {
            $(".line" + lineIndex + "").css({ opacity: 1, marginLeft: start.x + 'px', marginTop: start.y + 'px', position: "absolute", zIndex: 100 });
            $arrow.css({ opacity: 1 })
            $node.css({ opacity: 0.9 })

            if (end.x >= start.x) {
                exow = ow;
            } else {
                exow = -ow;

            }
            if (end.y >= start.y) { exoh = oh; } else { exoh = -oh; }

            if (size.x == "v" && size.y == "v") {
                style = 1;
                writenode();
                setline(start.x + exow, start.y, (start.x + end.x) / 2, start.y, $fline);
                setline((start.x + end.x) / 2, start.y, (start.x + end.x) / 2, end.y, $sline);
                setline((start.x + end.x) / 2, end.y, end.x - exow, end.y, $tline);
                writearrow()

            }
            else if (size.x == "h" && size.y == "h") {
                style = 2;
                writenode();
                setline(start.x, start.y + exoh, start.x, (start.y + end.y) / 2, $fline);
                setline(start.x, (start.y + end.y) / 2, end.x, (start.y + end.y) / 2, $sline);
                setline(end.x, (start.y + end.y) / 2, end.x, end.y - exoh, $tline);
                writearrow()

            }
            else if (size.x == "h" && size.y == "v") {
                style = 3;
                writenode();
                setline(start.x, start.y + exoh, start.x, end.y, $fline);
                setline(start.x, end.y, end.x - exow, end.y, $sline);
                setline(end.x, end.y, end.x, end.y, $tline)
                writearrow()

            }
            else if (size.x == "v" && size.y == "h") {
                style = 4;
                writenode();
                setline(start.x + exow, start.y, end.x, start.y, $fline);
                setline(end.x, start.y, end.x, end.y - exoh, $sline);
                setline(end.x, end.y, end.x, end.y, $tline)
                writearrow()

            }

        }

        function setline(sx, sy, ex, ey, $line) {
            if (sx == ex) {
                if ((start.x - end.x) * (start.y - end.y) < 0) {
                    sy = sy + 7;
                    ey = ey + 7;
                }
                if (sy > ey) {
                    $line.css({ left: (sx - start.x + (ow / 2)) + 'px', top: (sy - start.y + (oh / 2 - (sy - ey))) + 'px', width: 7 + 'px', height: (sy - ey) + 'px' });
                } else {
                    $line.css({ left: (sx - start.x + (ow / 2)) + 'px', top: (sy - start.y + oh / 2) + 'px', width: 7 + 'px', height: (ey - sy) + 'px' });
                }
                $line.css({ backgroundImage: 'url(/images/con4_left_bg1.GIF)' });
            } else if (sy == ey) {
                if (sx > ex) {
                    $line.css({ left: (sx - start.x + (ow / 2 - (sx - ex))) + 'px', top: (sy - start.y + oh / 2) + 'px', width: (sx - ex) + 'px', height: 7 + 'px' });
                } else {
                    $line.css({ left: (sx - start.x + ow / 2) + 'px', top: (sy - start.y + oh / 2) + 'px', width: (ex - sx) + 'px', height: 7 + 'px' });
                }
                $line.css({ backgroundImage: 'url(/images/con4_left_bg1.GIF)' });
            }

        }



        function writearrow() {
            var a, b;
            if (style == 1 && end.x >= start.x || style == 3 && end.x >= start.x) {
                a = -15 - ow / 2; b = -4; beginway = 1;
            } else if (style == 1 && end.x < start.x || style == 3 && end.x < start.x) {
                a = -4 + ow / 2; b = -4; beginway = 3;
            } else if (style == 2 && end.y >= start.y || style == 4 && end.y >= start.y) {
                a = -4; b = -15 - oh / 2; beginway = 2;
            } else if (style == 2 && end.y < start.y || style == 4 && end.y < start.y) {
                a = -4; b = -4 + oh / 2; beginway = 4;
            }
            $arrow.css({ left: (end.x - start.x + a + ow / 2) + 'px', top: (end.y - start.y + b + oh / 2) + 'px', width: 21 + 'px', height: 21 + 'px' });

            $arrow.find(".arrowout" + lineIndex + "").each(function () { this.src = '/images/jtbq' + beginway + '.png'; });
        }


        function writenode() {
            var a, b
            if (style == 1 && end.x >= start.x || style == 4 && end.x >= start.x) {
                a = 0 + ow / 2; b = -4;
            } else if (style == 1 && end.x < start.x || style == 4 && end.x < start.x) {
                a = -15 - ow / 2; b = -4;
            } else if (style == 2 && end.y >= start.y || style == 3 && end.y >= start.y) {
                a = -4; b = 0 + oh / 2;
            } else if (style == 2 && end.y < start.y || style == 3 && end.y < start.y) {
                a = -4; b = -15 - oh / 2;
            }
            $node.css({ left: (a + ow / 2) + 'px', top: (b + oh / 2) + 'px', width: 21 + 'px', height: 21 + 'px' });
            $node.find(".nodeout" + lineIndex + "").each(function () { this.src = '/images/jw.PNG'; });
        }
    }
    jQuery.fn.setlineFunctions = function (options) {
        settings = jQuery.extend({
            startx: 0,
            starty: 0,
            endx: 0,
            endy: 0,
            startsize: "v",
            endsize: "v",
            lineIndex: 0,


            style: 1
        }, options);
        setline(this, settings);
        return this;
    };

})(jQuery);

